package com.wzg.appframe.action;

import android.content.Intent;
import android.os.AsyncTask;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.wzg.appframe.net.HttpUtils;
import com.wzg.appframe.utils.GsonUtil;
import com.wzg.appframe.utils.TLog;
import com.wzg.appframe.utils.ToastUtil;
import com.wzg.appframe.view.BaseActivity;
import com.wzg.appframe.web.BaseWebActivity;

public class SettingAction extends BaseAction {


    public SettingAction(BaseActivity activity) {
        super(activity);
    }

    public void getAppInfoFromServer() {
        JsonObject p = new JsonObject();
        p.addProperty("type", 2);
        p.addProperty("pageIndex", 0);
        p.addProperty("pageSize", 1000);
        post("setting/getByType", p);
    }

    @Override
    public void postCallBack(String method, Object data) {
        TLog.d("method=" + method);
        TLog.d("data=" + GsonUtil.obj2String(data));
        JsonArray array = ((JsonElement) data).getAsJsonObject().get("list").getAsJsonArray();
        for (int i = 0; i < array.size(); i++) {
            JsonObject o = array.get(i).getAsJsonObject();
            String key = o.get("settingItemKey").getAsString();
            String value = o.get("settingItemValue").getAsString();
            setSharedPreferenData(key, value);
        }
        checkDataToH5Url();
    }

    public void checkDataToH5Url() {
        boolean flag = false;

        String H5Url = getSharedPreferenData(APP_H5_URL);
        TLog.d("H5Url=" + H5Url);
        if (H5Url == null || "".equals(H5Url.trim())) {

        } else {
            final String url = H5Url;
            new AsyncTask<Void, Void, Integer>() {

                @Override
                protected Integer doInBackground(Void... arg0) {
                    try {
                        //发送网络请求
                        String res = HttpUtils.doGet(url);
                        TLog.d("res=" + res);
                        if (res != null) {
                            return 1;
                        }


                    } catch (Exception e) {
                    }
                    return 0;
                }

                @Override
                protected void onPostExecute(Integer result) {
                    //根据结果执行代码
                    TLog.d("result=" + result);
                    if (result == 1) {
                        Intent intent = new Intent(activity, BaseWebActivity.class);
                        intent.putExtra(BaseWebActivity.PAGE, url);
                        activity.startActivity(intent);
                        activity.finish();//关闭页面
                    } else {
                        ToastUtil.show(url + " -> 地址不可用请联系管理员处理！");
                    }

                }
            }.execute();
        }
    }

    public void toH5Page() {
        String H5Url = getSharedPreferenData(APP_H5_URL);
//        String H5Url = BaseAction.H5_URL;
        TLog.d("tracy H5Url=" + H5Url);

        String errH5Url = getSharedPreferenData("ERR_URL");
        TLog.d("tracy errH5Url=" + errH5Url);

        if (H5Url != null && !"".equals(H5Url.trim()) && !H5Url.equals(errH5Url)) {
            BaseWebActivity.start(activity, BaseWebActivity.TYPE_VIEW,H5Url);
            activity.finish();//关闭页面
        }
    }
}
